package com.haha.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haha.common.ApiResponse;
import com.haha.constant.BaseCode;
import com.haha.domain.entity.user.User;
import com.haha.domain.qo.user.UserRegisterQo;
import com.haha.mapper.UserMapper;
import com.haha.redission.bloomFilter.BloomFilterService;
import com.haha.service.user.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zouwz5
 * @date 2025/10/3 10:13
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService   {

    @Autowired
    private BloomFilterService bloomFilterService;
    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public ApiResponse register(UserRegisterQo userRegisterQo) {

        // 已经注册
        if(bloomFilterService.contains(userRegisterQo.getPhone())){
            LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();
            lambdaQueryWrapper.eq(User::getPhone, userRegisterQo.getPhone());
            lambdaQueryWrapper.eq(User::getStatus, BaseCode.NOT_DELETED.getCode());
            User user = userMapper.selectOne(lambdaQueryWrapper);
            if (user != null){
                return ApiResponse.error("用户已经注册！");
            }
        }else {
            User user = new User();
            user.setName(userRegisterQo.getName());
            user.setPhone(userRegisterQo.getPhone());
            user.setPassword(userRegisterQo.getPassword());
            userMapper.insert(user);

            // 加入布隆过滤器
            bloomFilterService.add(userRegisterQo.getPhone());

        }
        return ApiResponse.ok("注册成功！");
    }
}
